//
//  MineViewController.m
//  Japanese+
//
//  Created by Jame on 15/10/20.
//  Copyright © 2015年 SSIC. All rights reserved.
//

#import "MineViewController.h"
#import "MineCell.h"


#import "WalletViewController.h"
#import "MyOrderViewController.h"
#import "EvaluationViewController.h"
#import "SuggestionViewController.h"
#import "IdentificationViewController.h"
#import "SettingViewController.h"
#import "InfoViewController.h"

@interface MineViewController () <UITableViewDataSource,UITableViewDelegate>

@end

@implementation MineViewController
{
    UIView *_userView;
    UIImageView *_userImage;
    UILabel *_nameLabel;
    UILabel *_signatureLabel;
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    
    MemberModel *_memberModel;
    TeacherModel *_teacherModel;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:@"个人"];

    [self initData];
    [self creatUserView];
    [self creatTableView];
}


- (void)viewWillAppear:(BOOL)animated
{
    [self loadUserInfo];
    [self reloadViewWithModel:_memberModel];
}

- (void)loadUserInfo
{
    UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
    [userInfo loadUserInfoFromSanbox];
    _memberModel = userInfo.memberModel;
    _teacherModel = userInfo.teacherModel;
}


- (void)reloadViewWithModel:(MemberModel *)model
{
    _nameLabel.text = model.name;
    if (model.signature.length == 0) {
        _signatureLabel.text = @"暂无签名";
    }else{
        _signatureLabel.text = model.signature;
    }
    [_userImage sd_setImageWithURL:[NSURL URLWithString:model.imgUrl] placeholderImage:ECIMAGENAME(@"header.png")];
}

- (void)initData
{
    _dataArray = [[NSMutableArray alloc] initWithObjects:@{
                                                           @"text":@"钱包管理",
                                                           @"image":@"mine_1.png",
                                                           },@{
                                                               @"text":@"我的订单",
                                                               @"image":@"mine_2.png",
                                                               },@{
                                                                   @"text":@"所有评价",
                                                                   @"image":@"mine_3.png",
                                                                   },@{
                                                                       @"text":@"意见反馈",
                                                                       @"image":@"mine_4.png",
                                                                       },@{
                                                                           @"text":@"认证设置",
                                                                           @"image":@"mine_5.png",
                                                                           },@{
                                                                               @"text":@"设置",
                                                                               @"image":@"mine_6.png",
                                                                               }, nil];
}



#pragma mark - creatUserView
- (void)creatUserView
{
    //userView
    _userView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, 180)];
    _userView.backgroundColor = BG_COLOR;
    [self.view addSubview:_userView];
    
    
    //headView
    UIView *headView = [UIView new];
    headView.backgroundColor = [UIColor whiteColor];
    headView.clipsToBounds = YES;
    headView.layer.cornerRadius = 100/2;
    [_userView addSubview:headView];
    
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_userView.mas_top).offset(10);
        make.centerX.mas_equalTo(_userView);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    
    
    //headImage
    _userImage = [UIImageView new];
    _userImage.image = ECIMAGENAME(@"header.png");
    _userImage.clipsToBounds = YES;
    _userImage.layer.cornerRadius = 96/2;
    [headView addSubview:_userImage];
    
    [_userImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headView);
        make.centerX.mas_equalTo(headView);
        make.size.mas_equalTo(CGSizeMake(96, 96));
    }];
    
    
    UIButton *button = [UIButton new];
    [headView addSubview:button];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headView);
        make.centerX.mas_equalTo(headView);
        make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    
    [button addTarget:self action:@selector(headClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
//    UIButton *editButton = [UIButton new];
//    [_userView addSubview:editButton];
//    
//    [editButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_userView.mas_top).offset(10);
//        make.right.mas_equalTo(_userView.mas_right).offset(-10);
//        make.size.mas_equalTo(CGSizeMake(80, 25));
//    }];
//    
//    
//    editButton.clipsToBounds = YES;
//    editButton.layer.cornerRadius = 3;
//    editButton.backgroundColor = NAVC_COLOR;
//    [editButton setTitle:@"编辑个人资料" forState:UIControlStateNormal];
//    [editButton setTitleColor:ECCOLOR(255, 255, 255, 1) forState:UIControlStateNormal];
//    editButton.titleLabel.font = [UIFont systemFontOfSize:12.f];
//    [editButton addTarget:self action:@selector(editClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //nameLabel
    _nameLabel = [UILabel new];
    _nameLabel.text = @"肖倪";
    _nameLabel.textColor = ECCOLOR(0, 0, 0, 1);
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.font = [UIFont systemFontOfSize:15.f];
    [_userView addSubview:_nameLabel];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_userView);
        make.top.mas_equalTo(headView.mas_bottom).offset(8);
    }];
    
    
    
    UIView *speraLine = [UIView new];
    speraLine.backgroundColor = ECCOLOR(0, 0, 0, .1f);
    [_userView addSubview:speraLine];
    
    [speraLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_userView);
        make.top.mas_equalTo(_nameLabel.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(ECSCREEN_W/2, 1));
    }];
    
    
    _signatureLabel = [UILabel new];
    _signatureLabel.text = @"Girl should be two thing:classy and fabulous.";
    _signatureLabel.textColor = ECCOLOR(152, 143, 139, 1);
    _signatureLabel.textAlignment = NSTextAlignmentCenter;
    _signatureLabel.font = [UIFont systemFontOfSize:13.f];
    [_userView addSubview:_signatureLabel];

    [_signatureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_userView);
        make.top.mas_equalTo(speraLine.mas_bottom).offset(5);
    }];
}



- (void)headClick:(UIButton *)button
{
    InfoViewController *vc = [[InfoViewController alloc] init];
    vc.memberId = _memberModel.member_Id;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


//- (void)editClick:(UIButton *)button
//{
//    EditInfoViewController *vc = [[EditInfoViewController alloc] init];
//    vc.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:vc animated:YES];
//}

- (void)creatTableView
{
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 244, ECSCREEN_W, ECSCREEN_H - 293) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    _tableView.backgroundColor = BG_COLOR;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    _tableView.tableFooterView = [[UIView alloc]init];
    
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    [self.view addSubview:_tableView];
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.f;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 5)];
    view.backgroundColor = BG_COLOR;
    return view;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseIdentifier = @"mineCell";
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (nil == cell) {
        NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"MineCell" owner:nil options:nil];
        for (NSObject *obj in arr) {
            if ([obj isKindOfClass:[MineCell class]]) {
                cell = (MineCell *)obj;
                if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
                    [cell setSeparatorInset:UIEdgeInsetsZero];
                }
                if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
                    [cell setLayoutMargins:UIEdgeInsetsZero];
                }
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                break;
            }
        }
    }
    
    [cell setCellWithModel:_dataArray WithIndexPath:indexPath];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (indexPath.section) {
        case 0:
        {
            WalletViewController *vc = [[WalletViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];

        }
            break;
        case 1:
        {
            MyOrderViewController *vc = [[MyOrderViewController alloc] init];
            vc.index = 0;
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            EvaluationViewController *vc = [[EvaluationViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            SuggestionViewController *vc = [[SuggestionViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            IdentificationViewController *vc = [[IdentificationViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 5:
        {
            SettingViewController *vc = [[SettingViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;

            
        default:
            break;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
